#!/usr/bin/env python2.7

# lidar_extra.py

import os, sys
#import re
#from datetime import datetime, timedelta
#from dateutil.parser import parse
import numpy as np
#import matplotlib
#matplotlib.use('Agg')
#import matplotlib.pyplot as plt
#from mpl_toolkits.basemap import Basemap
#from matplotlib import mlab
#from netCDF4 import Dataset
from metlib.lidar import *
from metlib.datetime import T, TD

paradir = '%s/parameters' % os.path.dirname(__file__)

def lidar_extra(lidard):
    if isinstance(lidard, tuple):
        lidarnamed = {
                'yl':'Yuenlong Lidar',
                'yl2':'Yuenlong2 Lidar',
                'stk':'ShaTauKok Lidar',
                'ph':'Pump House Lidar',
                'iap':'IAP Lidar',
                'IAP Lidar':'IAP Lidar',
                'Yuenlong Lidar':'Yuenlong Lidar',
                'Yuenlong2 Lidar':'Yuenlong2 Lidar',
                'ShaTauKok Lidar':'ShaTauKok Lidar',
                'Pump House Lidar':'Pump House Lidar'
                }
        resod = {
                'Yuenlong Lidar':'30',
                'Yuenlong2 Lidar':'7.5',
                'Pump House Lidar':'15',
                'IAP Lidar':'30' 
                }
        tmplidarname, dt = lidard
        lidarname = lidarnamed[tmplidarname]
        if lidarname == 'ShaTauKok Lidar':
            resolution = '7.5' if dt > T(20071127) else '15'
        else:
            resolution = resod[lidarname]
        lidard = {
                'lidarname':lidarname,
                'start_datetime':dt,
                'resolution':resolution
                }
    res = dict()
    res['elev_angle'] = 75.0
    res['chn'] = 0
    dt = lidard['start_datetime']
    if lidard['lidarname'] == "ShaTauKok Lidar":
        if 'resolution' not in lidard:
            if 14 < lidard['bin_size'] < 16:
                resolution = "15"
            elif 7 < lidard['bin_size'] < 8:
                resolution = "7.5"
            lidard['resolution'] = resolution
        else:
            resolution = lidard['resolution']
        res['resolution'] = resolution
        if resolution == "7.5":
            res['valid_index'] = 25
            res['imax20km'] = 2667
            res['distance'] = 7.49481145 * np.arange(8000) - 70.0
            olfile = 'overlap1.dat'
            if dt < T(20071107):
                apfile = 'ap_fit_20071026_small.dat'
            elif dt < T(20071201):
                apfile = 'ap_fit_20071118_small2.dat'
            elif dt < T(20071218):
                apfile = 'ap_fit_20071214_small3.dat'
            elif dt < T(20071221):
                apfile = 'ap_fit_20071218_large1.dat'
            elif dt < T(20080119):
                apfile = 'ap1_75.dat'
            elif dt < T(20080313):
                apfile = 'ap_fit_20080127_small.dat'
            elif dt < T(20080411):
                apfile = 'ap_fit_20080321_small.dat'
            elif dt < T(20080601):
                apfile = 'ap_fit_20080411_sublarge.dat'
            elif dt < T(20081226):
                apfile = 'ap_fit_20081006_small.dat'
            elif dt < T(20090216):
                apfile = 'ap_fit_20090204_sublarge.dat'
            elif dt < T(20090304):
                apfile = 'ap_fit_20090219_small.dat'
            elif dt < T(20091016):
                apfile = 'ap_fit_20090403_sublarge.dat'
            else:
                apfile = 'ap_fit_20100405.dat'
        elif resolution == '15':
            res['valid_index'] = 12
            res['imax20km'] = 1334
            res['distance'] = 14.98962 * np.arange(4000) - 70.0
            apfile = 'ap1_15.dat'
            olfile = 'overlap1_15.dat'
        res['norm_vmin'] = -3.0
        res['norm_vmax'] = 1.0
        res['ap'] = np.loadtxt('%s/stk/%s' % (paradir, apfile))
        res['ol'] = np.loadtxt('%s/stk/%s' % (paradir, olfile))
        res['building_height'] = 0.0
    elif lidard['lidarname'] == "Yuenlong Lidar":
        res['valid_index'] = 7
        res['norm_vmin'] = -3.0
        res['norm_vmax'] = 1.0
        res['imax20km'] = 667
        res['distance'] = 29.9792458 * np.arange(2000) -44.96887
        if dt < T(20040101):
            res['ap'] = np.loadtxt('%s/yl/afterpulse1.dat' % paradir)
            res['ol'] = np.loadtxt('%s/yl/overlap1.dat' % paradir)
        elif dt < T(20040701):
            res['ap'] = np.loadtxt('%s/yl/afterpulse2.dat' % paradir)
            res['ol'] = np.loadtxt('%s/yl/overlap1.dat' % paradir)
        else:
            res['ap'] = np.loadtxt('%s/yl/afterpulse3.dat' % paradir)
            res['ol'] = np.loadtxt('%s/yl/overlap2.dat' % paradir)
        res['building_height'] = 28.0
        res['resolution'] = '30'
    elif lidard['lidarname'] == "Yuenlong2 Lidar":
        res['valid_index'] = 25
        res['imax20km'] = 2667
        res['distance'] = 7.49481145 * np.arange(8000) - 70.0
        res['ap'] = np.loadtxt('%s/yl2/ap1_75.dat' % paradir)
        res['ol'] = np.loadtxt('%s/yl2/overlap1.dat' % paradir)
        if dt < T(20120101):
            res['norm_vmin'] = -3.0
            res['norm_vmax'] = -1.0
        elif dt < T(20120601):
            res['norm_vmin'] = -3.25
            res['norm_vmax'] = -1.25
        elif dt < T(20130101):
            res['norm_vmin'] = -3.5
            res['norm_vmax'] = -1.5
        else:
            res['norm_vmin'] = -3.75
            res['norm_vmax'] = -1.75
        res['building_height'] = 28.0
        res['resolution'] = '7.5'
    elif lidard['lidarname'] == "Pump House Lidar":
        res['valid_index'] = 211
        res['imax20km'] = 1334
        res['distance'] = 14.98962 * np.arange(4000) - 2975.44
        if dt < T(20110201):
            res['ap'] = np.loadtxt('%s/ph/ap_fit_20110119.dat' % paradir).transpose()
        elif dt < T(20120410):
            res['ap'] = np.loadtxt('%s/ph/ap_20120413.dat' % paradir).transpose()
        elif dt < T(20121014):
            res['ap'] = np.loadtxt('%s/ph/ap_fit_20120511.dat' % paradir).transpose()
        else:
            res['ap'] = np.loadtxt('%s/ph/ap_fit_20130322.dat' % paradir).transpose()
        res['ol'] = np.loadtxt('%s/ph/ol1.dat' % paradir)
        if dt < T(20110701):
            res['norm_vmin'] = -2.0
            res['norm_vmax'] = 0.0
            res['norm_vmin0'] = -2.8
            res['norm_vmax0'] = -0.8
        elif dt < T(20120101):
            res['norm_vmin'] = -2.4
            res['norm_vmax'] = -0.4
            res['norm_vmin0'] = -3.05
            res['norm_vmax0'] = -1.05
        elif dt < T(20120701):
            res['norm_vmin'] = -2.7
            res['norm_vmax'] = -0.7
            res['norm_vmin0'] = -3.2
            res['norm_vmax0'] = -1.2
        elif dt < T(20130101):
            res['norm_vmin'] = -3.0
            res['norm_vmax'] = -1.0
            res['norm_vmin0'] = -3.35
            res['norm_vmax0'] = -1.35
        else:
            res['norm_vmin'] = -3.3
            res['norm_vmax'] = -1.3
            res['norm_vmin0'] = -3.5
            res['norm_vmax0'] = -1.5
        res['chn'] = 1
        res['building_height'] = 0.0
        res['resolution'] = '15'
    elif lidard['lidarname'] == "IAP Lidar":
        res['valid_index'] = 105
        res['norm_vmin'] = -2.0
        res['norm_vmax'] = 0.0
        res['norm_vmin0'] = -2.5
        res['norm_vmax0'] = -0.5
        res['imax20km'] = 767
        res['distance'] = 29.97924 * np.arange(2000) - 2982.935
        res['ap'] = np.loadtxt('%s/iap/ap2.dat' % paradir).transpose()
        res['ol'] = np.loadtxt('%s/iap/ol1.dat' % paradir)
        res['elev_angle'] = 90.0
        if dt > T(20120101):
            res['ap'][0] = res['ap'][0] * 0.05
            res['ap'][1] = res['ap'][1] * 0.10
        res['chn'] = 1
        res['building_height'] = 0.0
    elif lidard['lidarname'] == "AMPLE532 Lidar":
        res['valid_index'] = 10 + 7
        res['norm_vmin'] = 2.5
        res['norm_vmax'] = 5
        res['norm_vmin0'] = 2.5
        res['norm_vmax0'] = 5
        res['imax20km'] = 676
        res['distance'] = 29.97924 * (np.arange(2048) - 10 + 0.5)
        res['ap'] = np.loadtxt('%s/ample/ap532_1.dat' % paradir).transpose()
        res['ol'] = np.loadtxt('%s/ample/ol532_1.dat' % paradir)
        res['elev_angle'] = 90.0
        res['chn'] = 0
    elif lidard['lidarname'] == "AMPLE355 Lidar":
        res['valid_index'] = 17 + 11
        res['norm_vmin'] = 2.5
        res['norm_vmax'] = 5
        res['norm_vmin0'] = 2.5
        res['norm_vmax0'] = 5
        res['imax20km'] = 676
        res['distance'] = 19.186717312 * (np.arange(4096) - 17 + 0.5)
        res['ap'] = np.loadtxt('%s/ample/ap355_1.dat' % paradir).transpose()
        res['ol'] = np.loadtxt('%s/ample/ol355_1.dat' % paradir)
        res['elev_angle'] = 90.0
        res['chn'] = 0
        res['building_height'] = 0.0
    res['sin_elev'] = np.sin(np.deg2rad(res['elev_angle']))
    return res

if __name__ == '__main__':
    pass
